﻿//////////////////////////////////////////////////////////////////////////
// test tree module
// author :zp
// date : 2012-3-14
// depend: none;
//////////////////////////////////////////////////////////////////////////
$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "test1.json",
        dataType: "json",
        success: function(data) {
            var dataOut = {};
            dataOut = ParseA2Json(data);
            $.fn.zTree.init($("#testGrid"), {}, dataOut);
        },
        error: function(msg) {
            alert("load data error!");
        }

    })
});

function ParseA2Json(dataIn) {
    // the name map
    var nameMap = { "expanded": "open",
        "text": "name",
        children: "children"
    }
    var dataOut = {};
    if (typeof dataIn == "string") {
        dataIn = eval(dataIn);
    }
    if (dataIn instanceof Array) {
        dataOut = [];
        var len = dataIn.length;
        for (var j = 0; j < len; ++j) {
            var curOut = {};
            var curIn = dataIn[j];
            curOut = ParseA2Json(curIn);
            dataOut.push(curOut);
        }
    } else {
        for (var att in dataIn) {
            if (typeof dataIn[att] != "function") {
                if (nameMap[att] != undefined) {
                    if (att == "children") {
                        dataOut[nameMap[att]] = [];
                        var curChildren = dataIn[att];
                        var len = curChildren.length;
                        for (var i = 0; i < len; ++i) {
                            var curDataOut = ParseA2Json(curChildren[i]);
                            dataOut[nameMap[att]].push(curDataOut);
                        }
                    } else {
                        dataOut[nameMap[att]] = dataIn[att];
                    }
                }
            }
        }
    }
    return dataOut;
}